package com.ag.lwc.dao;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.ag.bean.TbUser;
import com.ag.dao.TbUserDeptDAO;
import com.ag.zhh.vo.UserDept;
import com.ag.zhh.vo.UserDeptContact;
/**
 * 用户部门的dao
 * @author user
 *
 */
public class LwcUserDeptDao extends TbUserDeptDAO{
	
	/**
	 * 将药监局的第一层子部门查出来 
	 * uid是腰间内部，协调部门，等的id
	 * @return
	 */
	public List<UserDept> findFirstLevel(String udid)
	{
		List<UserDept> result=null;
		try {
			result = new LinkedList<UserDept>();
			Session session  = this.getSessionFactory().openSession();
			System.out.println("***********************\n"+udid);
			Set<UserDept> depts = new HashSet<UserDept>();
			UserDept mydept = (UserDept) session.get(UserDept.class, udid);
			depts = mydept.getTbUserDepts();
			for(UserDept dept:depts)
			{
				System.out.println("部门："+dept.getUdName());
				result.add(dept);
			}
			
	//		result = session.createQuery("from UserDept where this.userDept.udId =:udId").setString("udId", udid).list();
			session.close();
		} catch (HibernateException e) {
		
			e.printStackTrace();
		}
		return result;
	}
	public List<UserDept> findNextLevel(String udid)
	{
		List<UserDept> result=null;
		try {
			result = new LinkedList<UserDept>();
			Session session  = this.getSessionFactory().openSession();
			System.out.println("%^%^$^%$^%$^%$^%$\n"+udid);
			Set<UserDept> depts = new HashSet<UserDept>();
			UserDept mydept = (UserDept) session.get(UserDept.class, udid);
			depts = mydept.getTbUserDepts();
			for(UserDept dept:depts)
			{
				System.out.println("部门："+dept.getUdName());
				result.add(dept);
			}
		//	result = session.createQuery("from UserDept where userDept.udId =:udId").setString("udId", udid).list();
			session.close();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public List<UserDeptContact> findUserDeptContact(String udid)
	{
		List<UserDeptContact> result = null;
		Session session  = this.getSessionFactory().openSession();
		
		UserDept dept = (UserDept) session.get(UserDept.class, udid);
		Set<UserDeptContact> users = dept.getTbUsers();
		result = new LinkedList<UserDeptContact>();
		for(UserDeptContact user:users)
		{
			result.add(user);
			System.out.println("人的id："+user.getUId());
		}
		session.close();
		return result;
	}



}
